package com.woniuxy.repay.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.entity.BorrowDemand;
import com.woniuxy.repay.entity.PageBean;
import com.woniuxy.entity.RepaymentManage;
import com.woniuxy.repay.mapper.RepaymentManageMapper;
import com.woniuxy.repay.service.RepaymentManageService;
import com.woniuxy.repay.tools.ServiceTool;
import com.woniuxy.service.business.BorrowDemandService;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author shichen
 * @create 2021-11-13-10:49
 * @description
 */
@Slf4j
@Service
public class RepaymentManageServiceImpl implements RepaymentManageService {

    @Resource
    RepaymentManageMapper repaymentManageMapper;

    /**
     * openfeign调用借款信息
     */
    @Resource
    BorrowDemandService borrowDemandService;



    @Override
    public PageBean selectByCondition(RepaymentManage repaymentManage,Integer pageIndex) {
        List<RepaymentManage> repaymentManages = repaymentManageMapper.selectByCondition(
                repaymentManage, ServiceTool.pageIndexTransition(pageIndex), 5);
        Integer total = repaymentManageMapper.selectCountByCondition(repaymentManage);
        for (RepaymentManage manage : repaymentManages) {
            int borrowInfo = manage.getBorrowInfo();
            ResponseResult responseResult = borrowDemandService.select(borrowInfo);
            log.debug("发送过来的消息是 : {}",responseResult.getData());

            if(responseResult.getStatus() == ResultStatus.HAVE_PERMS){
                LinkedHashMap data = (LinkedHashMap) responseResult.getData();
                log.debug(data.toString());
                ObjectMapper objectMapper=new ObjectMapper();
                BorrowDemand borrowDemand = objectMapper.convertValue(data, BorrowDemand.class);
                log.debug("转换后的效果 : {}",borrowDemand);
                manage.setBorrowDemand(borrowDemand);
            }
        }
        return new PageBean(total,5,pageIndex,repaymentManages);
    }

    @Override
    public void updateByCondition(RepaymentManage repaymentManage) {
        repaymentManageMapper.updateByCondition(repaymentManage);
        // 更新缓存
        //return selectAllRepaymentManage(1);

    }

    @Override
    public void insert(RepaymentManage repaymentManage) {

        repaymentManageMapper.insertByCondition(repaymentManage);

    }
}
